package com.qf.controller;

import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import com.qf.feign.SearchFeign;
import com.qf.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author 汤奇
 * @version V1.0
 * @Project BookPro
 * @Package com.qf.controller
 * @Description:
 * @Date 2022/7/6 13:47
 */
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private EurekaClient eurekaClient;

    /**
     * restTemplate是Spring封装的java服务端发送http协议的远程调用工具
     */
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private SearchFeign searchFeign;

    /**
     * 测试远程调用底层原理
     * @return
     */
    @RequestMapping("/consumer")
    public String customer(){
        //1.通过eurekaClient获取到search微服务的对象信息
//        InstanceInfo info = eurekaClient.getNextServerFromEureka("QF-SEARCH", false);

        //2.从搜索微服务对象信息中获取搜索微服务的ip和端口号
//        String url = info.getHomePageUrl();

        //3.通过restTemplate对象发送http协议远程调用
        String result = restTemplate.getForObject("http://QF-SEARCH/search/test", String.class);
        System.out.println(result);
        //4.返回
        return result;
    }

    @RequestMapping("/testFeign")
    public String testFeign(){
        String test = searchFeign.test();
        System.out.println(test);
        return test;
    }

    @RequestMapping("/findUser")
    public User findUserById(){
        User user = searchFeign.findUserById(1);
        System.out.println(user);
        return user;
    }
}
